import { fabric } from 'fabric';
import { v4 as uuidv4 } from 'uuid';
import TextPlugin from './TextPlugin';
import ShapePlugin from './ShapePlugin';
import LayerPlugin from './LayerPlugin';
import HistoryPlugin from './HistoryPlugin';
import AIPlugin from './AIPlugin';
import ImagePlugin from './ImagePlugin';

/**
 * 图形插件 - 提供添加基本图形的功能
 */
export default class ShapePlugin {
    static pluginName = 'ShapePlugin';
    static apis = ['addRectangle', 'addCircle', 'addTriangle', 'addPolygon', 'addLine'];

    constructor(canvas, editor) {
        this.canvas = canvas;
        this.editor = editor;
        this.defaultFill = '#3498db';
        this.defaultStroke = '#2980b9';
    }

    // 添加矩形
    addRectangle(options = {}) {
        const rect = new fabric.Rect({
            left: 100,
            top: 100,
            width: 100,
            height: 100,
            fill: this.defaultFill,
            stroke: this.defaultStroke,
            strokeWidth: 1,
            id: uuidv4(),
            name: '矩形',
            ...options
        });

        this.canvas.add(rect);
        this.canvas.setActiveObject(rect);
        this.canvas.renderAll();

        this.editor.emit('shape:added', rect);
        return rect;
    }

    // 添加圆形
    addCircle(options = {}) {
        const circle = new fabric.Circle({
            left: 100,
            top: 100,
            radius: 50,
            fill: this.defaultFill,
            stroke: this.defaultStroke,
            strokeWidth: 1,
            id: uuidv4(),
            name: '圆形',
            ...options
        });

        this.canvas.add(circle);
        this.canvas.setActiveObject(circle);
        this.canvas.renderAll();

        this.editor.emit('shape:added', circle);
        return circle;
    }

    // 添加三角形
    addTriangle(options = {}) {
        const triangle = new fabric.Triangle({
            left: 100,
            top: 100,
            width: 100,
            height: 100,
            fill: this.defaultFill,
            stroke: this.defaultStroke,
            strokeWidth: 1,
            id: uuidv4(),
            name: '三角形',
            ...options
        });

        this.canvas.add(triangle);
        this.canvas.setActiveObject(triangle);
        this.canvas.renderAll();

        this.editor.emit('shape:added', triangle);
        return triangle;
    }

    // 添加多边形
    addPolygon(points = [], options = {}) {
        const defaultPoints = [
            { x: 0, y: 0 },
            { x: 50, y: 0 },
            { x: 75, y: 50 },
            { x: 50, y: 100 },
            { x: 0, y: 100 },
            { x: -25, y: 50 }
        ];

        const polygon = new fabric.Polygon(points.length ? points : defaultPoints, {
            left: 100,
            top: 100,
            fill: this.defaultFill,
            stroke: this.defaultStroke,
            strokeWidth: 1,
            id: uuidv4(),
            name: '多边形',
            ...options
        });

        this.canvas.add(polygon);
        this.canvas.setActiveObject(polygon);
        this.canvas.renderAll();

        this.editor.emit('shape:added', polygon);
        return polygon;
    }

    // 添加直线
    addLine(points = [0, 0, 100, 100], options = {}) {
        const line = new fabric.Line(points, {
            stroke: this.defaultStroke,
            strokeWidth: 2,
            id: uuidv4(),
            name: '直线',
            ...options
        });

        this.canvas.add(line);
        this.canvas.setActiveObject(line);
        this.canvas.renderAll();

        this.editor.emit('shape:added', line);
        return line;
    }

    // 设置默认填充色
    setDefaultFill(color) {
        this.defaultFill = color;
    }

    // 设置默认描边色
    setDefaultStroke(color) {
        this.defaultStroke = color;
    }
}

// 导出所有插件
export {
    TextPlugin,
    ShapePlugin,
    LayerPlugin,
    HistoryPlugin,
    AIPlugin,
    ImagePlugin
};